package com.example.java_chatroom.api;

import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.FriendMapper;
import com.example.java_chatroom.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

// 处理好友信息
@Slf4j
@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @RequestMapping("/friendList")
    public Object getFriendList(HttpServletRequest req) {
        // 1. 先从会话中, 获取到 userId
        HttpSession session = req.getSession(false);
        if(session == null) {
            log.info("[getFriendList] session 不存在");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            log.info("[getFriendList] user 不存在");
            return new ArrayList<Friend>();
        }

        // 根据 userId 查询数据库
        List<Friend> list = friendMapper.selectFriendList(user.getUserId());
        return list;
    }
}
